// pages/index/index.js
const db = wx.cloud.database();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        banners: [{
                id: 1,
                imageUrl: 'https://636c-cloud1-1gwj5n7fc52da891-1361339536.tcb.qcloud.la/images/5555.jpg?sign=1a564ff347f3e08336a9c9a25c30b547&t=1749031061'
            },
            {
                id: 2,
                imageUrl: 'https://636c-cloud1-1gwj5n7fc52da891-1361339536.tcb.qcloud.la/images/6666.jpg?sign=9e829087324964d3049c02e5c9da7836&t=1749025152'
            }
        ],
        ips: [], // 盲盒IP列表
        isLoading: true, // 加载状态
        isLoggedIn: false,
        typeList: []
    },

    navigateToMorePage: function () {
        wx.navigateTo({
            url: '../ip1page/ip1page'
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        this.loadIps();
        this.loadTypeList()
        const isLoggedIn = wx.getStorageSync('isLoggedIn');
        console.log(isLoggedIn);
        this.setData({
            isLoggedIn
        });
    },
    // 加载盲盒衍生品分类
    loadTypeList() {
        db.collection('derivative_type').get()
            .then(res => {
                console.log(res);
                this.setData({
                    typeList: res.data,
                    isLoading: false
                });
            })
            .catch(err => {
                console.error('加载盲盒衍生品失败', err);
                wx.showToast({
                    title: '盲盒衍生品加载失败',
                    icon: 'none'
                });
            });
    },
    // 加载盲盒IP数据
    loadIps() {
        db.collection('blindbox_ips')
            .where({
                isHot: true
            })
            .get()
            .then(res => {
                console.log(res);
                this.setData({
                    ips: res.data,
                    isLoading: false
                });
            })
            .catch(err => {
                console.error('加载IP数据失败', err);
                wx.showToast({
                    title: '数据加载失败',
                    icon: 'none'
                });
            });
    },

    // 跳转到搜索页面
    goToSearch() {
        wx.navigateTo({
            url: '/pages/search/search'
        });
    },

    // 跳转到IP页面
    goToIpPage(e) {
        const ipId = e.currentTarget.dataset.ipid;
        const ipName = e.currentTarget.dataset.ipname;
        console.log(e);
        wx.navigateTo({
            url: `/pages/ipList/index?ipId=${ipId}&ipName=${ipName}`
        });
    },

    // 跳转到其他IP页面
    goToOtherPage(e) {
        console.log(e);
        wx.navigateTo({
            url: '/pages/other/other'
        });
    },

    // 跳转到衍生品页面
    goToDerivatives(e) {
        console.log(e);
        const derivativeid = e.currentTarget.dataset.derivativeid;
        if (derivativeid) {
            wx.navigateTo({
                url: `/pages/derivatives/derivatives?derivativeId=${derivativeid}`
            });
        } else {
            wx.navigateTo({
                url: `/pages/derivatives/derivatives`
            });
        }

    },
    // 跳转登录页
    navigateToLogin: function () {
        wx.navigateTo({
            url: '/pages/login/login'
        });
    },
    
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        const isLoggedIn = wx.getStorageSync('isLoggedIn');
        console.log(isLoggedIn);
        this.setData({
            isLoggedIn
        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})